package jzqb.controller;


import haha.life.common.CutPageBean;
import haha.life.common.SysTool;
import haha.life.normal.RandomUtils;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.ApiOperation;
import jzqb.bo.QBFWAuditBo;
import jzqb.bo.QBFWBo;
import jzqb.common.Result;
import jzqb.po.LCPo;
import jzqb.po.QBFWPo;
import jzqb.service.IQBFWService;
import jzqb.vo.QBFWAuditVo;
import jzqb.vo.QBFWUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Description: 情报服务
 *
 * @author BaiGe
 * @date: 2021/6/21 16:37
 * @Version 1.0
 */
@RestController
@RequestMapping("/qbfw")
public class QBFWController {
    @Autowired
    private IQBFWService qbfwService;

    //情报服务上传附件临时存放地址
    @Value("${filePath.fileTemPResource}")
    private String fileTemPResource;

    @ApiOperation(value = "情报服务检索按钮")
    @PostMapping("/complexQueryQBFW")
    public Result complexQueryQBFW(@RequestBody QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        qbfwAuditBo.setStart(qbfwAuditBo.getPage(), qbfwAuditBo.getPageSize());
        CutPageBean<QBFWPo> cutPageBean = qbfwService.complexQueryQBFW(qbfwAuditBo, request);
        return Result.succ(cutPageBean);
    }

    @ApiOperation(value = "展示情报服务-->展示自己的服务申请")
    @PostMapping("/queryQBFW")
    public Result queryQBFW(@RequestBody QBFWBo qbfwBo, HttpServletRequest request) {
//        String currentUserId = TokenUtils.getCurrentUserId(request);
//        qbfwBo.setApplicant(currentUserId);
        qbfwBo.setStart(qbfwBo.getPage(), qbfwBo.getPageSize());
        CutPageBean<QBFWPo> cutPageBean = qbfwService.queryQBFW(qbfwBo, request);
        return Result.succ(cutPageBean);
    }

    @ApiOperation(value = "展示一条情报服务")
    @PostMapping("/showSingleQBFW")
    public Result showSingleQBFW(@RequestBody Map<String, Integer> map) {
        Integer qbfwId = map.get("qbfwId");
        QBFWPo qbfwPo = qbfwService.showSingleQBFW(qbfwId);
        return Result.succ(qbfwPo);
    }


    @ApiOperation(value = "展示情报服务-待审核列表")
    @PostMapping("/showQBFWAuditList")
    public Result showQBFWAuditList(@RequestBody QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        qbfwAuditBo.setStart(qbfwAuditBo.getPage(), qbfwAuditBo.getPageSize());
        CutPageBean<QBFWPo> cutPageBean = qbfwService.showQBFWAuditList(qbfwAuditBo, request);
        return Result.succ(cutPageBean);
    }

    @ApiOperation(value = "展示情报服务-已审核列表")
    @PostMapping("/showHaveReadQBFWAuditList")
    public Result showHaveReadQBFWAuditList(@RequestBody QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        qbfwAuditBo.setStart(qbfwAuditBo.getPage(), qbfwAuditBo.getPageSize());
        CutPageBean<QBFWPo> cutPageBean = qbfwService.showHaveReadQBFWAuditList(qbfwAuditBo, request);
        return Result.succ(cutPageBean);
    }


    @ApiOperation(value = "展示单条 情报服务-审核列表")
    @PostMapping("/showSingleQBFWAudit")
    public Result showSingleQBFWAudit(@RequestBody Map<String, Integer> map) {
        Integer qbfwId = map.get("qbfwId");
        QBFWAuditVo qbfwAuditVo = qbfwService.showSingleQBFWAudit(qbfwId);
        return Result.succ(qbfwAuditVo);
    }

    @ApiOperation(value = "情报服务流程审批")
    @PostMapping("/qbfwLcAudit")
    public Result qbfwLcAudit(@RequestBody LCPo lcPo, HttpServletRequest request) {
        Object updateLc = qbfwService.qbfwLcAudit(lcPo, request);
        return Result.succ(updateLc);
    }

    @ApiOperation(value = "新建情报服务")
    @PostMapping("/addQBFW")
    public Result addQBFW(@RequestBody QBFWPo qbfwPo) {
        String dept = qbfwPo.getApplicationDept();
        if ("".equals(dept) || dept == null) {
            return Result.fail("请选择一个部门");
        }
        Boolean checkDeptLeader = qbfwService.checkDeptLeader(dept);
        if (!checkDeptLeader) {
            return Result.fail("该部门无领导,请重新选择");
        }
        Boolean addQBFW = qbfwService.addQBFW(qbfwPo);
        if (addQBFW) {
            return Result.succ("添加情报服务成功");
        } else {
            return Result.fail("添加情报服务失败");
        }

    }

    /**
     * 上传文件
     *
     * @param file 上传的文件
     * @return 文件存放路径
     */
    @RequestMapping(value = "/qbfwUploading", method = {RequestMethod.POST})
    public String qbfwUploading(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        String nowTime = SysTool.getCurrentTime(SysTool.dateFormat_date);
        String basePath = fileTemPResource + "\\" + currentUserId + "\\" + nowTime;
        String jsonStr = qbfwService.qbfwUploading(file, basePath);
        return jsonStr;
    }

    /**
     * 批量上传文件
     *
     * @param request 当前请求
     * @return 文件各自的存放路径
     */
    @CrossOrigin
    @RequestMapping(value = "/qbfwBatchUploading", method = RequestMethod.POST)
    public Object qbfwBatchUploading(HttpServletRequest request) {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        String nowTime = SysTool.getCurrentTime(SysTool.dateFormat_date);
        String randDir = RandomUtils.getRandomString(8);
        String allFolderPath = fileTemPResource + "\\" + currentUserId + "\\" + nowTime + "\\" + randDir;
        Object obj = qbfwService.qbfwBatchUploading(allFolderPath, request);
        return obj;
    }


    /**
     * 删除上传的文件
     *
     * @param map 参数对象
     * @param
     * @return
     */
    @PostMapping("/qbfwDelFile")
    public Result qbfwDelFile(@RequestBody Map map) {
        Boolean delBoolean = qbfwService.qbfwDelFolder(map);
        return Result.succ(delBoolean);
    }

    @ApiOperation(value = "删除情报服务")
    @PostMapping("/deleteQBFW")
    public Result deleteQBFW(@RequestBody QBFWPo qbfwPo) {
        Boolean delBoolean = qbfwService.delQBFWAndAnnexPath(qbfwPo);
        if (delBoolean) {
            return Result.succ("删除成功!");
        } else {
            return Result.fail("删除失败!");
        }

    }

    @ApiOperation(value = "修改情报服务")
    @PostMapping("/updateQBFW")
    public Result updateQBFW(@RequestBody QBFWBo qbfwBo) {
        Boolean updateBoolean = qbfwService.updateQBFW(qbfwBo);
        if (updateBoolean) {
            return Result.succ("修改成功!");
        } else {
            return Result.fail("修改失败!");
        }
    }

    @ApiOperation(value = "修改被打回的情报服务")
    @PostMapping("/updateRejectQBFW")
    public Result updateRejectQBFW(@RequestBody QBFWBo qbfwBo) {
        Boolean updateRejectQBFW = qbfwService.updateRejectQBFWO(qbfwBo);
        if (updateRejectQBFW) {
            return Result.succ("修改成功!");
        } else {
            return Result.fail("修改失败!");
        }
    }

    @ApiOperation(value = "获取当前信息")
    @PostMapping("/getCurrentUser")
    public Result getCurrentUser(HttpServletRequest request) {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        QBFWUserVo currentUser = qbfwService.getCurrentUser(currentUserId);
        return Result.succ(currentUser);
    }

}
